package com.example.wxpro.controller;

import com.example.wxpro.modle.MessageData;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wx")
public class WxController {
    private static Logger logger= LoggerFactory.getLogger(WxController.class);
    @Autowired
    private WxMpService mpService;

    @GetMapping()
    public String checkSignature(@RequestParam String timestamp, @RequestParam String nonce, @RequestParam String signature ) throws WxErrorException {
        // this.mpService.getWxMpConfigStorage().getAppId();
        if (this.mpService.checkSignature(timestamp, nonce, signature)){
            logger.info("check token success");
            return signature;
        }else{
            return "";
        }
    }
    @PostMapping(produces = "application/xml; charset=UTF-8")
    public String receiveData(@RequestParam String ToUserName, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String signature ) throws WxErrorException {
        // this.mpService.getWxMpConfigStorage().getAppId();
         
        if (this.mpService.checkSignature(timestamp, nonce, signature)){
            logger.info("check token success");
            return signature;
        }else{
            return "";
        }
    }
}
